package com.erb.controller.zwj;

import com.erb.constants.IntegralConstants;
import com.erb.entity.Pager;
import com.erb.entity.User;
import com.erb.entity.integral;
import com.erb.service.ljy.signService;
import com.erb.service.zwj.ISignInRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sign-in")
public class SignInController {
	/**
	 * 记录列表每页显示的条数
	 */
	private static final int SIGN_IN_HISTORY_PAGE_NUMBER = 8;
	@Resource
	private ISignInRecordService signInRecordService;
	@Resource
	private signService signService;

	@GetMapping("/")
	public String signIn() {
		return "common/sign-in";
	}

	/**
	 * 获取指定月已签到的日期
	 *
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getSignedInDateOfMonth")
	public List<Integer> getSignedInDateOfMonth(HttpSession session, @RequestParam Integer month) {
		User user = (User) session.getAttribute("user");

		if (user == null) {
			return signInRecordService.getSignedInDateOfMonth(1, month);
		}
		return signInRecordService.getSignedInDateOfMonth(user.getId(), month);
	}

	/**
	 * 签到
	 *
	 * @return
	 */
	@ResponseBody
	@PostMapping("/attestation")
	public Map<String, Object> attestation(HttpServletRequest request, HttpSession session, @RequestParam Integer date) {
		Map<String, Object> map = new HashMap<>(1);

		int userId;
		User user = (User) session.getAttribute("user");
		if (user == null) {
			userId = 1;
		} else {
			userId = user.getId();
		}
		/*
		签到：
			判断今天是否已签到：
				是：返回提示
				否：查询是否是连续签到
					是：计算奖励积分，保存记录
					否：
						保存记录
		 */
		/*User user = (User) session.getAttribute("user");

		// 已签到
		if(signInRecordService.checkSignedInOfDate(user.getId(),date)){
			map.put("code",1);
			map.put("msg","您已签到！");
			return map;
		}*/
		// Map<?, ?> resultMap = restTemplate.getForObject("http://localhost:8080/erb/doSign?userId=" + userId, Map.class);

		map.put("code", 1);
		map.put("data", "签到成功！");
		return map;
	}

	/**
	 * 签到的历史信息
	 *
	 * @param session
	 * @return
	 */
	@ResponseBody
	@GetMapping("/signInHistory")
	public Map<String, Object> signInHistory(HttpSession session) {
		/*
		1.连续签到天数
			通过ID查询次数记录表
		2.本月签到次数
			通过ID和本月区间查询次数
		3.总共签到次数
			通过ID查询记录次数
		4.累计签到奖励
			循环所有记录求和
		 */
		Map<String, Object> resultMap = new HashMap<>(4);
		User user = (User) session.getAttribute("user");

		// 连续天数
		Integer continueDayCount = signService.getcDays(user.getId());
		resultMap.put("continueDay", (continueDayCount == null || continueDayCount <= 0 ? 0 : continueDayCount));

		// 本月已签到的记录
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		List<Integer> signedInDateOfMonth = signInRecordService.getSignedInDateOfMonth(user.getId(), calendar.get(Calendar.MONTH) + 1);
		resultMap.put("signedInRecordList", signedInDateOfMonth);

		// 用户签到的所有记录
		// 默认8条
		Pager pager = new Pager();
		pager.setPageSize(SIGN_IN_HISTORY_PAGE_NUMBER);
		// 查询总数
		pager = signInRecordService.findRecordsOfUser(pager, user.getId());
		// pager对象中存储了需要显示的记录列表
		resultMap.put("pager", pager);

		// 用户所得所有积分个数
		int integralCount = 0;
		for (integral i : (List<integral>) pager.getData()) {
			integralCount += IntegralConstants.INTEGRAL_SCORE_MAP.get(i.getScoreCount());
		}
		resultMap.put("userSignInIntegralNumber", integralCount);

		return resultMap;
	}

	/**
	 * 签到历史分页查询数据
	 *
	 * @param session
	 * @return
	 */
	@ResponseBody
	@GetMapping("/signInHistoryForPage")
	public Pager signInHistoryForPage(HttpSession session, Pager pager) {
		Map<String, Object> resultMap = new HashMap<>(4);
		User user = (User) session.getAttribute("user");

		pager.setPageSize(SIGN_IN_HISTORY_PAGE_NUMBER);
		pager = signInRecordService.findRecordsOfUser(pager, 1);

		return pager;
	}
}
